package org.kailu.book.controller;

import org.kailu.book.entity.LanguageEntity;
import org.kailu.book.service.LanguageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/**
 * @author viet.ch
 * @generated May 24, 2014
 * @from VN
 */

@Controller
public class LanguageController {

	@Autowired
	private LanguageService languageService;
	
	@RequestMapping("/language")
	public String getData(ModelMap map) {
		map.addAttribute("languageList",languageService.getAll());
		return "language";
	}
	
	@RequestMapping(value = "/language/saveorupdate", method = RequestMethod.GET)
	public String initData(ModelMap map) {
		map.addAttribute("language",new LanguageEntity());
		return "language_new";
	}
	
	@RequestMapping(value = "/language/saveorupdate", method = RequestMethod.POST)
    public String add(@ModelAttribute(value="language") LanguageEntity languageEntity, BindingResult result, ModelMap map)
    {
		languageService.saveOrUpdate(languageEntity);
		return "redirect:/language";
    }
	
	@RequestMapping(value = "/language/saveorupdate/{languageId}", method = RequestMethod.GET)
    public String update(@PathVariable("languageId") int languageId, ModelMap map)
    {
		map.addAttribute("language", languageService.get(languageId));
		return "language_new";
    }
	
	@RequestMapping(value = "/language/delete/{languageId}", method = RequestMethod.GET)
    public String delete(@PathVariable("languageId") int languageId)
    {
		this.languageService.delete(languageId);
		return "redirect:/language";
    }
}
